//
//  KRRecommentCell.m
//  cookBook
//
//  Created by kuroken on 16/2/5.
//  Copyright © 2016年 kuroken. All rights reserved.
//

#define MAS_SHORTHAND

#import "KRRecommentCell.h"
#import <Masonry.h>
#import <UIImageView+WebCache.h>
#import "KRRecommend.h"

// cell重用标识
static NSString * const KRRecommentID = @"recommentID";

NSUInteger const KRAvatarImageHW = 25;

@interface KRRecommentCell ()

/** 图片 iamgeView */
@property (nonatomic, weak) UIImageView *imgView;
/** 标题 label */
@property (nonatomic, weak) UILabel *titleLabel;
/** 头像 iamgeView */
@property (nonatomic, weak) UIImageView *avatarImageView;
/** 用户名＋回复数 label */
@property (nonatomic, weak) UILabel *collectionLabel;
/** 内容预览 label */
@property (nonatomic, weak) UILabel *previewLabel;
@end

@implementation KRRecommentCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    KRRecommentCell *cell = [tableView dequeueReusableCellWithIdentifier:KRRecommentID];
    if (!cell) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:KRRecommentID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // <#初始化代码#>
        
        [self setupSubview];
        [self addConstraint];
    }
    return self;
}

- (void)setRecomment:(KRRecommend *)recomment
{
    _recomment = recomment;
    [self.imgView sd_setImageWithURL:[NSURL URLWithString:recomment.img] placeholderImage:[UIImage imageNamed:@"default_r172-118"]];
    self.imgView.clipsToBounds = YES;
    [self.titleLabel setText:recomment.title];
    [self.avatarImageView sd_setImageWithURL:[NSURL URLWithString:recomment.avatar] placeholderImage:[UIImage imageNamed:@"default_p80"]];
    [self.collectionLabel setText:recomment.collection];
    [self.previewLabel setText:recomment.previewContent];
}


/**
 *  初始化子控件
 */
- (void)setupSubview
{
    /** 图片 iamgeView */
    UIImageView *imgView = [[UIImageView alloc] init];
    imgView.contentMode = UIViewContentModeScaleAspectFill;
    imgView.clipsToBounds = YES;
    [self.contentView addSubview:imgView];
    self.imgView = imgView;

    /** 标题 label */
    UILabel *titleLabel = [[UILabel alloc] init];
    [titleLabel setFont:[UIFont systemFontOfSize:16]];
    [self.contentView addSubview:titleLabel];
    self.titleLabel = titleLabel;

    /** 头像 iamgeView */
    UIImageView *avatarImageView = [[UIImageView alloc] init];
    avatarImageView.layer.cornerRadius = KRAvatarImageHW * 0.5;
    avatarImageView.clipsToBounds = YES;
    [self.contentView addSubview:avatarImageView];
    self.avatarImageView = avatarImageView;

    /** 用户名＋回复数 label */
    UILabel *collectionLabel = [[UILabel alloc] init];
    [collectionLabel setFont:[UIFont systemFontOfSize:13]];
    [collectionLabel setTextColor:[UIColor lightGrayColor]];
    [self.contentView addSubview:collectionLabel];
    self.collectionLabel = collectionLabel;

    /** 内容预览 label */
    UILabel *previewLabel = [[UILabel alloc] init];
    [previewLabel setFont:[UIFont systemFontOfSize:13]];
    [previewLabel setTextColor:[UIColor lightGrayColor]];
    [self.contentView addSubview:previewLabel];
    self.previewLabel = previewLabel;

}

/**
 *  添加约束
 */
- (void)addConstraint
{
    [self.imgView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).with.offset(10);
        make.centerY.equalTo(self.contentView);
        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(100, 70)]);
    }];
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imgView);
        make.left.equalTo(self.imgView.right).with.offset(10);
        make.right.equalTo(self.contentView).with.offset(-10);
    }];
    
    [self.avatarImageView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.bottom).with.offset(5);
        make.left.equalTo(self.titleLabel);
        make.size.equalTo([NSValue valueWithCGSize:CGSizeMake(KRAvatarImageHW, KRAvatarImageHW)]);
    }];
    
    [self.collectionLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.avatarImageView);
        make.left.equalTo(self.avatarImageView.right).with.offset(10);
    }];
    
    [self.previewLabel makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.imgView);
        make.left.equalTo(self.titleLabel);
        make.right.equalTo(self.titleLabel);
    }];
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
